package huizhi.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * TODO
 *
 * @author YuXD
 * @since 2023/12/25
 */
public class SocketServer2 {

    public static void main(String[] args) throws IOException {
        System.out.println("我是SocketServer");
        // 启动服务端，监听8888端口
        ServerSocket server = new ServerSocket(8888);
        // 服务端待命，等待客户端连接。在套接字连接之前，都处于阻塞状态
        Socket accept = server.accept();

        // 获取客户端输入字节流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        String clientMsg = bufferedReader.readLine();
        System.out.println(clientMsg);

        // 从套接字获取字节输出流
        OutputStream outputStream = accept.getOutputStream();

        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bufferedWriter.write("hello, client！");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        outputStream.close();
        accept.close();
        server.close();
    }

}
